/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Category;

import java.sql.*;
import java.util.Vector;

/**
 *
 * @author mishra
 */
public class Query {

    static Connection con = null;

    public static int add(CategoryBean cb) throws SQLException {
        int i = 0, k = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("insert into category values(categoryid.nextval,?,sysdate)");
            pre.setString(1, cb.getCategory());
            i = pre.executeUpdate();
            if (i != 0) {
                k = getMaxId();
            }

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }

    public static int getMaxId() throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select max(category_id) from category");
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                i = rs.getInt(1);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }

    public static CategoryBean viewSpecific(String cname, String val) throws SQLException {
        CategoryBean cb = new CategoryBean();
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from category where " + cname + "=?");
            pre.setString(1, val);
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                cb.setCategoryId(rs.getInt(1));
                cb.setCategory(rs.getString(2));
                cb.setEntryDate(rs.getDate(3));
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return cb;
    }

    public static Vector viewAll() throws SQLException {

        Vector<Object> list = new Vector<Object>();
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from category order by category_id");
            ResultSet rs = pre.executeQuery();
            while (rs.next()) {
                CategoryBean cb = new CategoryBean();
                cb.setCategoryId(rs.getInt(1));
                cb.setCategory(rs.getString(2));
                cb.setEntryDate(rs.getDate(3));
                list.add(cb);
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return list;
    }
    
    public static int edit(CategoryBean cb) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("update category set category_name=?,entrydate=sysdate where category_id=?");
            pre.setString(1, cb.getCategory());
            pre.setInt(2, cb.getCategoryId());
            i = pre.executeUpdate();

        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }
    public static int delete(int id) throws SQLException
        {
            int i=0;
            try
            {
              con=DBConnection.Connection.getConnection();
              PreparedStatement pre=con.prepareStatement("delete from category where category_id=?");
              pre.setInt(1,id);
              i=pre.executeUpdate();
            }
            catch(Exception e)
            {
               return i; 
            }
            finally
            {
              con.close();  
            }
            return i;
        }
}
